﻿using HandyControl.Data;
using MaterialDesignThemes.Wpf;
using MyMeeting.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace MyMeeting.Views
{
    /// <summary>
    /// MeetingView.xaml 的交互逻辑
    /// </summary>
    public partial class MeetingView : UserControl
    {
        Point pt;
        public MeetingView()
        {
            InitializeComponent();
            _closeTimer = new DispatcherTimer
            {
                Interval = TimeSpan.FromMilliseconds(500)
            };
            _closeTimer.Tick += CloseTimer_Tick;
            this.Loaded += MeetingView_Loaded;
        }

        private void MeetingView_Loaded(object sender, RoutedEventArgs e)
        {
            pt = MainMenuButton.PointToScreen(new Point(0, 0));
        }

        // 计时器触发时关闭菜单
        private void CloseTimer_Tick(object sender, EventArgs e)
        {
            _closeTimer.Stop();
            PrimaryMenu.IsOpen = false;
        }

        // 鼠标重新进入 Popup 时取消关闭
        private void PopupContent_MouseEnter(object sender, MouseEventArgs e)
        {
            _closeTimer.Stop();
        }
        private DispatcherTimer _closeTimer;

        // 鼠标离开按钮区域时隐藏菜单
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            // 获取鼠标相对于窗口的位置
            Point mousePos = e.GetPosition(this);
            mousePos = this.PointToScreen(mousePos);

            var ptMain = PrimaryMenu.PointToScreen(new Point(0, 0));
            if (IsContainsPoint(mousePos, pt, MainMenuButton.ActualWidth, MainMenuButton.ActualHeight))
            {
                _closeTimer.Stop();
                if(PrimaryMenu.IsOpen==false)
                {

                    PrimaryMenu.IsOpen = true;
                }
            }

            if (PrimaryMenu.IsOpen == true)
            {
                var ptMainStack = PrimaryStack.PointToScreen(new Point(0, 0));

                var m1 = MenuItem1.PointToScreen(new Point(0, 0));
                if (IsContainsPoint(mousePos, m1, MenuItem1.ActualWidth, MenuItem1.ActualHeight))
                {
                    _closeTimer.Stop();
                    SubMenu1.IsOpen = true;
                }


                if (SubMenu1.IsOpen == true)
                {
                    var pt3 = subMe.PointToScreen(new Point(0, 0));

                    if (!IsContainsPoint(mousePos, pt, MainMenuButton.ActualWidth, MainMenuButton.ActualHeight) && !IsContainsPoint(mousePos, ptMainStack, PrimaryStack.ActualWidth + 10, PrimaryStack.ActualHeight) && !IsContainsPoint(mousePos, pt3, subMe.ActualWidth, subMe.ActualHeight))
                    {
                        _closeTimer.Start();
                    }
                    else
                    {
                        if (!IsContainsPoint(mousePos, pt3, subMe.ActualWidth, subMe.ActualHeight))
                        {
                            if (!IsContainsPoint(mousePos, m1, MenuItem1.ActualWidth, MenuItem1.ActualHeight))
                            {
                                SubMenu1.IsOpen = false;
                            }
                        }
                    }
                }
                else
                {
                    if (!IsContainsPoint(mousePos, pt, MainMenuButton.ActualWidth, MainMenuButton.ActualHeight)&&!IsContainsPoint(mousePos, ptMainStack, PrimaryStack.ActualWidth, PrimaryStack.ActualHeight) && !IsContainsPoint(mousePos, pt, MainMenuButton.ActualWidth, MainMenuButton.ActualHeight))
                    {
                        _closeTimer.Start();
                    }
                    else
                    {
                        _closeTimer.Stop();
                    }
                }
            }
        }

        private bool IsContainsPoint(Point mousePos, Point pt, double width, double height)
        {
            if (mousePos.X >= pt.X && mousePos.X <= pt.X + width &&
               mousePos.Y >= pt.Y && mousePos.Y <= pt.Y + height)
            {
                return true;
            }
            return false;
        }
    }
}
